﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace A5Lib
{
    public class User
    {
        public User()
        {
            Appliances = new List<Appliance>();
            ApplianceNames = new HashSet<string>();

        }
        public string Username { get; set; }
        public string Password { get; set; }
        public List<Appliance> Appliances { get; set; }
        public HashSet<string> ApplianceNames { get; set; }
        public void addAppliance(Appliance app)
        {
            if (ApplianceNames.Add(app.Name))
            {
                Appliances.Add(app);
            }
        }
        public bool addAppliance(string applianceName)
        {
            if (ApplianceNames.Add(applianceName))
            {
                Appliance app = new Appliance(applianceName);
                Appliances.Add(app);
                return true;
            }
            return false;
        }

        public bool deleteAppliance(string applianceName)
        {
            if (ApplianceNames.Remove(applianceName))
            {
                for (int i = 0; i < Appliances.Count; i++)
                {
                    if (Appliances[i].Name == applianceName)
                    {
                        Appliances.RemoveAt(i);
                        return true; //only return true if successfully removed
                    }
                }
            }
            return false;
        }

        public Appliance getApplianceByName(string applianceName)
        {
            for (int i = 0; i < Appliances.Count; i++)
            {
                if (Appliances[i].Name == applianceName)
                {
                    return Appliances[i]; 
                }
            }
            return null;
        }
    }
}
